package com.Zhengxin.smarthome.service;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import com.Zhengxin.smarthome.SmartHomeUIActivity;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class GetInformationFromPC extends Service{
	private DatagramSocket socket;
	private NotificationManager mNotificationManager;
	private Notification mNotification;
	private PendingIntent mPendingIntent;
	private Intent intent;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		System.out.println("GetInformationFromPC start");
		try {
			socket = new DatagramSocket(9528);
//			byte[] buf = new byte[1024];
//			final DatagramPacket dp1=new DatagramPacket(buf,buf.length);
//	        Runnable runnable=new Runnable(){
//
//				public void run() {
//					// TODO Auto-generated method stub
//					while(true){
//						try{
//							Thread.sleep(100);
//							socket.receive(dp1);
//							int length=dp1.getLength();
//							String message= new String(dp1.getData(),0,length);
//							String ip=dp1.getAddress().getHostAddress();
//							//if(!InetAddress.getLocalHost().getHostAddress().equals(ip))text.append(ip+"\n"+message+"\n");
//							//vsBar.setValue(vsBar.getMaximum());
//							System.out.println("message"+message);
//							//control(message);
//						}catch(IOException e){e.printStackTrace();
//						}catch(InterruptedException e){e.printStackTrace();}
//						}
//					}
//				};
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		System.out.println("onstert");
		byte[] buf = new byte[1024];
		final DatagramPacket dp1=new DatagramPacket(buf,buf.length);
        Runnable runnable=new Runnable(){

			public void run() {
				// TODO Auto-generated method stub
				while(true){
					try{
						System.out.println("start run");
						Thread.sleep(100);
						socket.receive(dp1);
						int length=dp1.getLength();
						String message= new String(dp1.getData(),0,length);
						String ip=dp1.getAddress().getHostAddress();
						//if(!InetAddress.getLocalHost().getHostAddress().equals(ip))text.append(ip+"\n"+message+"\n");
						//vsBar.setValue(vsBar.getMaximum());
						
						notification(message);
					}catch(IOException e){e.printStackTrace();
					}catch(InterruptedException e){e.printStackTrace();}
					}
				}
			};
			new Thread(runnable).start();
        	
        }

	private void notification(String message) {
		// TODO Auto-generated method stub
		System.out.println("nomessage"+message);
			System.out.println("notification");
			mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
			intent= new Intent(GetInformationFromPC.this,SmartHomeUIActivity.class);
			mPendingIntent=PendingIntent.getActivity(GetInformationFromPC.this, 0, intent, 0);
			
			mNotification=new Notification();
			mNotification.icon=R.drawable.arrow_down_float;
			mNotification.tickerText=message;
			mNotification.defaults=Notification.DEFAULT_SOUND;
			mNotification.setLatestEventInfo(GetInformationFromPC.this, message, message, mPendingIntent);
			mNotificationManager.notify(0,mNotification);
								
	}
      
	}


